// 这份代码专门处理逻辑层的操作
const planDao = require('../dao/planDao');

const planService = {
    /*
     * 获取计划列表
     */
    async getPlanList(uid) {
        let result;
        try {
            let res = await planDao.getPlanList(uid);
            if (res) {
                result = { success: true, msg: "查询计划成功", planList: res };
            } else {
                result = { success: false, msg: "查询计划失败" };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询计划失败" };
        }
        return result;
    },
    /*
     * 添加计划
     */
    async addPlan(uid, plan) {
        let { title, description, count } = plan;
        let result;
        try {
            let res = await planDao.addPlan(uid, title, description, count);
            if (res.insertId) {
                result = { success: true, msg: "添加计划成功" };
            } else {
                result = { success: false, msg: "添加计划失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，添加计划失败" };
        }
        return result;
    },
    /*
     * 删除计划
     */
    async deletePlan(uid, planId) {
        let result;
        try {
            let res = await planDao.deletePlan(uid, planId);
            if (res.affectedRows >= 1) {
                result = { success: true, msg: "删除计划成功" };
            } else {
                result = { success: false, msg: "删除计划失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，删除计划失败" };
        }
        return result;
    },
    /*
     * 修改计划内容
     */
    async updatePlan(uid, planId, title, description, count) {
        let result;
        try {
            let res = await planDao.updatePlan(uid, planId, title, description, count);
            if (res.affectedRows >= 1) {
                result = { success: true, msg: "修改计划成功" };
            } else {
                result = { success: false, msg: "修改计划失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，修改计划失败" };
        }
        return result;
    },
    /*
     * 获取计划详情
     */
    async getPlanInfo(uid, planId) {
        let result;
        try {
            let res = await planDao.getPlanInfo(uid, planId);
            if (res.length) {
                result = { success: true, msg: "查询计划详情成功", planInfo: res[0] };
            } else {
                result = { success: false, msg: "查询计划详情失败" };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询计划详情失败" };
        }
        return result;
    },
    /*
     * 计划打卡
     */
    async checkinPlan(uid, planId, day) {
        let result;
        // // 判断是不是今天的日期
        // if (new Date(day).getDate() !== new Date().getDate()) {
        //     return { success: false, msg: "不是今日的日期" };
        // }
        try {
            let res = await planDao.checkinPlan(uid, planId, day);
            if (res.affectedRows) {
                result = { success: true, msg: "计划打卡成功" };
            } else {
                result = { success: false, msg: "今日已打卡" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，计划打卡失败" };
        }
        return result;
    },
};

module.exports = planService;